//
//  DHHomeLabelCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/4/13.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHHomeLabelCell.h"
#import "DHProductListController.h"

#pragma mark - LabelItem
@interface LabelItem : UIControl
@property (strong, nonatomic) UIImageView *imageView; //图片
@property (strong, nonatomic) UILabel *titleLabel; //标题
@end

@implementation LabelItem

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if (!self.imageView) {
            self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 13)];
            self.imageView.layer.cornerRadius = 8;
            [self addSubview:self.imageView];
        }
        if (!self.titleLabel) {
            self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.imageView.bottom, frame.size.width, 13)];
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            self.titleLabel.font = [UIFont systemFontOfSize:11];
            self.titleLabel.textColor = [UIColor colorWithHexString:@"#575757"];
            [self addSubview:self.titleLabel];
        }
    }
    return self;
}

@end

#pragma mark - DHHomeLabelCell
@implementation DHHomeLabelCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)updateCellWithArray:(NSArray *)arr{
    
    self.arr = arr;
    
    int itemSpace = 10;
    CGFloat itemW = 100;
    
    self.scrollView.contentSize = CGSizeMake(arr.count * itemW + (arr.count-1) * itemSpace, self.scrollView.height);
    
    //TODO::
    for (int i = 0; i < arr.count; i++) {
        NSDictionary *dic = arr[i];
        LabelItem *item = [[LabelItem alloc] initWithFrame:CGRectMake(i * (itemW + itemSpace), 0, itemW, self.scrollView.height)];
        [item.imageView dh_setImageURLString:[dic objectForKey:@"icon"]];
        item.titleLabel.text = [dic objectForKey:@"title"];
        item.tag = i;
        item.userInteractionEnabled = YES;
        [item addTarget:self action:@selector(labelAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:item];
    }
}

- (void)labelAction:(LabelItem *)item{
    NSDictionary *model = [self.arr objectAtIndex:item.tag];
    DHProductListController *vc = [[DHProductListController alloc] init];
    vc._id = [model objectForKey:@"_id"];
    vc.paramsKey = @"label_id";
    vc.navTitle = [model objectForKey:@"title"];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
